package com.yuan.util.system;

import com.yuan.util.file.FileUtil;
import com.yuan.util.string.ReuseStringBuilder;

/**
 * 获取Java运行时信息
 * @author YouYuan
 * @version 1.0
 * @CreateDate 2018/7/11 14:25
 * @UpdateDate 2018/7/11 14:25
 */
public class RuntimeInfo {

    private Runtime currentRuntime = Runtime.getRuntime();

    public final Runtime getRuntime() {
        return this.currentRuntime;
    }

    public final long getMaxMemory() {
        return this.currentRuntime.maxMemory();
    }

    public final long getTotalMemory() {
        return this.currentRuntime.totalMemory();
    }

    public final long getFreeMemory() {
        return this.currentRuntime.freeMemory();
    }

    public final long getUsableMemory() {
        return this.currentRuntime.maxMemory() - this.currentRuntime.totalMemory() + this.currentRuntime.freeMemory();
    }

    public String toString() {
        StringBuilder builder = ReuseStringBuilder.getStringBuilder();
        builder.append("Max Memory:    " + FileUtil.readableFileSize(getMaxMemory())).append("\n");
        builder.append("Usable Memory:     " + FileUtil.readableFileSize(getUsableMemory())).append("\n");
        builder.append("Total Memory:     " + FileUtil.readableFileSize(getTotalMemory())).append("\n");
        builder.append("Free Memory:     " + FileUtil.readableFileSize(getFreeMemory()));
        return builder.toString();
    }
}
